<?php
/**
 * Created by fengkl
 * User: Administrator
 * Date: 2018年5月25日 10:44:20
 * 
 */

namespace app\common\logic;

use app\common\model\Store;
use app\common\model\Order;
use app\common\validate;
use think\Db;

class OrderLogic 
{
    /**
     * 订单列表
     */
    public function getList($map,$order,$size)
    {
        $ormodel = new Order();
        //$field = 'a.*, b.us_nickname, b.us_tel,c.or_pd_name,or_pd_total';
        $field = 'a.*, b.us_nickname, b.us_tel';
        $info = $ormodel->getList($map,$order,$size,$field);
        return $info;
    }

    public function getListNpg($map,$order,$size)
    {
        $ormodel = new Order();
        $field = 'a.*, b.us_nickname, b.us_tel';
        $info = $ormodel->getListNpg($map,$order,$size,$field);
        foreach ($info as $k =>$v){

//            $order_detail = model('order_detail')->where('or_id',$v['id'])->select();
            $info[$k]['pdInfo'] = model('order_detail')->where('or_id',$v['id'])->field('or_id,pd_id')->select();
            foreach ($info[$k]['pdInfo'] as $key =>$value){
                $info[$k]['pdInfo'][$key]['pd_name'] = model('order_detail')
                    ->where('or_id',$value['or_id'])->where('pd_id',$value['pd_id'])
                    ->value('or_pd_name');
                $info[$k]['pdInfo'][$key]['pd_pic'] = model('order_detail')
                    ->where('or_id',$value['or_id'])->where('pd_id',$value['pd_id'])
                    ->value('or_pd_head_pic');
                $info[$k]['pdInfo'][$key]['shopping_id'] = model('order_detail')
                    ->where('or_id',$value['or_id'])->where('pd_id',$value['pd_id'])
                    ->value('shopping_id');
                $info[$k]['pdInfo'][$key]['or_pd_total'] = model('order_detail')
                    ->where('or_id',$value['or_id'])->where('pd_id',$value['pd_id'])
                    ->value('or_pd_total');
                //or_pd_name
            }

        }
//        halt($info);
        /*foreach ($info as $k => $v) {
            $v['ptel'] = model("User")->where('id', $v['us_pid'])->value('us_tel');
        }*/
        //var_dump($info);exit;
        return $info;
    }

    /**
     * 商品信息
     * author fengkl
     * time 2018年5月24日18:05:42
     * @return mixed
     */
    public function getOneOrder($id)
    {
        $pdmodel = new Order();
        $map['a.id'] = $id;
        $field = 'a.id,a.addr_id,b.pd_id , a.sku_id, b.or_pd_name, b.or_pd_num, b.or_pd_price,b.or_pd_pic,b.pd_color,b.ca_id, b.pd_spec, c.province, c.city, c.area, c.addr_detail, c.addr_tel, c.addr_receiver';
        $info = $pdmodel->getOneOrder($map,$field);
        return $info;
    }
   
    /**
     * 订单一个详情商品信息
     * author fengkl
     * time 2018年6月13日 10:10:45
     * @return mixed
     */
    public function getOneDetail($id)
    {
        $ormodel = new Order();
        $map['b.id'] = $id;
        $field = 'a.id,b.pd_id , b.or_pd_name, b.or_pd_num, b.or_pd_price,b.or_pd_pic,b.or_pd_content, b.pd_color, b.pd_spec , c.province, c.city, c.area, c.addr_detail, c.addr_tel, c.addr_receiver,a.or_express,a.or_express_num';
        $info = $ormodel->getOneDetail($map,$field);
        return $info;
    }


    /**
     * 商品添加和修改
     * author fengkl
     * time 2018年5月24日 15:48:18
     * @return mixed
     * code为1为修改
     */
    public function saveOrder($data,$code = '')
    {
        //var_dump($data);exit();
        $rel = array();     
        if($code == 1){
            //修改操作              
            $map['id'] = $data['id'];   
            $rell = model('Order')->updateInfo($map,$data);
            if($rell){
                $rel['code'] = 1;
                $rel['msg'] = '修改成功！';
            }else{
                $rel['code'] = 0;
                $rel['msg'] = '您没有作出修改！';
            }
            return $rel;
        }
        $data['or_add_time'] = date('Y-m-d H:i:s');
        //添加操作      
        $rell = model('Order')->addInfo($data);
        if($rell){
            $rel['code'] = 1;
            $rel['msg'] = '添加成功！';
        }else{
            $rel['code'] = 0;
            $rel['msg'] = '添加失败！';
        }
        return $rel;
    }

    /**
     * 分类添加
     * author fengkl
     * time 2018年5月23日 17:38:34
     * @return mixed
     * 
     */
    public function saveCate($data)
    {
        $validate = validate('Verify');
        $rel = array();
        $rst = $validate->scene('addCate')->check($data);
        if (!$rst) {
            $rel['code'] = 0;
            $rel['msg'] = $validate->getError();
            return $rel;
        }
        $data['ca_add_time'] = date('Y-m-d H:i:s');
        //添加操作      
        $rell = model('Cate')->addInfo($data);
        if($rell){
            $rel['code'] = 1;
            $rel['msg'] = '添加成功！';
        }else{
            $rel['code'] = 0;
            $rel['msg'] = '添加失败！';
        }
        return $rel;

    }

    /**
     * 订单添加
     * author fengkl
     * time 2018年6月5日 11:55:03
     * @return mixed
     * 
     */
    public function addInfo($data)
    {
        $validate = validate('Verify');
        $rel = array();
        $rst = $validate->scene('addCate')->check($data);
        if (!$rst) {
            $rel['code'] = 0;
            $rel['msg'] = $validate->getError();
            return $rel;
        }
        $data['ca_add_time'] = date('Y-m-d H:i:s');
        //添加操作      
        
    }

    /**
     * 判断订单发货时间是否超过规定日期
     * author fengkl
     * time 2018年6月12日 17:50:59
     * @return mixed
     * 
     */
    public function delay(){
        $order = new Order;
        $list = $order->select();
    }
    /**
     * 二级分销奖励发放  订单发放
     * @Author   sunpf
     * @DateTime 2019-4-16 16:49:45
        
     * @param    [type]                   $total         [订单总金额]
     
     * @param    [type]                   $or_id         [订单号]
     * @return   [type]                                  [发放奖励]
     */
    public function gaveAward($total,$or_id){

        $usid = model('order')->where('id',$or_id)->value('us_id');
        $ca_id = model('order')->where('id',$or_id)->value('ca_id');
        $where['id'] = $usid;
        //$usid = model('user')->where($where)->value('id');
        $user_first_id = model('user')->where($where)->value('us_pid');//直推id
        $where1['id'] = $user_first_id;
        $user_secend_id = model('user')->where($where1)->value('us_pid');//二级id
        $map['us_pid'] = $user_first_id;
        //$map['us_is_order'] = 1;//是否下单成为VIP用户
        $array_ids = model('user')->where($map)->field('id')->select();
        //$percent = 0;
        if ($user_first_id == 0) {
            return [
                    'code'=> 1,
                    'msg' =>'顶级用户不用发奖励'
                ];    
        }
        //return $array_ids;
        if ($ca_id == 10) {                         //复购奖励
            $award4 = cache('setting')['award4']/100;
            $award5 = cache('setting')['award5']/100;
            $rel1 = model('user')->where('id',$user_first_id)->setInc('us_secend_get',($total * $award4));
            $relall = model('user')->where('id',$user_first_id)->setInc('us_all_get',($total * $award4));
            if ($rel1) {
                $data_w['or_id'] = $or_id;
                $data_w['us_id'] = $user_first_id;
                $data_w['wa_num'] = $total * $award4;
                $data_w['wa_type'] = 2;
                $data_w['wa_note'] = "复购奖励发放";
                $data_w['add_time'] = date('Y-m-d H:i:s');
                $wa1_rel1 = model('Wallet')->addInfo($data_w);
            }else{
                return [
                    'code'=>0,
                    'msg' =>'发送直推复购奖励失败1'
                ];    
            }
            if ($user_secend_id != 0) {
                $rel2 = model('user')->where('id',$user_secend_id)->setInc('us_secend_get',($total * $award5));
                $relall = model('user')->where('id',$user_secend_id)->setInc('us_all_get',($total * $award5));
                if ($rel2) {
                    $data_w['or_id'] = $or_id;
                    $data_w['us_id'] = $user_secend_id;
                    $data_w['wa_num'] = $total * $award5;
                    $data_w['wa_type'] = 2;
                    $data_w['wa_note'] = "复购奖励发放";
                    $data_w['add_time'] = date('Y-m-d H:i:s');
                    $wa1_rel2 = model('Wallet')->addInfo($data_w);
                }else{
                    return [
                        'code'=>0,
                        'msg' =>'发送间接复购奖励失败2'
                    ];      
                }
            }
            if (isset($wa1_rel2)) {
                if ($wa1_rel2 && $wa1_rel1) {
                    return [
                        'code'=>1,
                        'msg' =>'发放成功'
                    ];
                }else{
                    return [
                        'code'=>0,
                        'msg' =>'复购奖励发放失败'
                    ];
                }
            }else{
                if ($wa1_rel1) {
                    return [
                        'code'=>1,
                        'msg' =>'发放成功'
                    ];
                }else{
                    return [
                        'code'=>0,
                        'msg' =>'复购奖励发放失败'
                    ];
                }
            }

            // return $array_ids;
        }elseif ($ca_id == 11) {                    //报单奖励
            $award1 = cache('setting')['award1']/100;
            $award2 = cache('setting')['award2']/100;
            $award3 = cache('setting')['award3']/100;
            foreach ($array_ids as $k => $v) {
                if ($v['id'] == $usid) {
                    $sort = $k + 1;
                    if ($sort%3 == 0) {
                        $percent = $award2;
                    }else{
                        $percent = $award1;
                    }
                }
            }
            $rel = model('user')->where('id',$user_first_id)->setInc('us_first_get',($total * $percent));
            $relall = model('user')->where('id',$user_first_id)->setInc('us_all_get',($total * $percent));
            // return $rel;
            if ($rel) {
                $data_w['or_id'] = $or_id;
                $data_w['us_id'] = $user_first_id;
                $data_w['wa_num'] = $total * $percent;
                $data_w['wa_type'] = 1;
                $data_w['wa_note'] = "报单奖励发放";
                $data_w['add_time'] = date('Y-m-d H:i:s');
                $wa1_rel1 = model('Wallet')->addInfo($data_w);
            }else{
                return [
                    'code'=>0,
                    'msg' =>'发送直推报单奖励失败1',
                ];               
            }
            if ($user_secend_id != 0) {
                $rel = model('user')->where('id',$user_secend_id)->setInc('us_first_get',($total * $award3));
                $relall = model('user')->where('id',$user_secend_id)->setInc('us_all_get',($total * $award3));
                if ($rel) {
                    $data_w['or_id'] = $or_id;
                    $data_w['us_id'] = $user_secend_id;
                    $data_w['wa_num'] = $total * $award3;
                    $data_w['wa_type'] = 1;
                    $data_w['wa_note'] = "报单奖励发放";
                    $data_w['add_time'] = date('Y-m-d H:i:s');
                    $wa1_rel2 = model('Wallet')->addInfo($data_w);
                }else{
                    return [
                        'code'=>0,
                        'msg' =>'发送间接报单奖励失败2'
                    ];
                }
            }
            if (isset($wa1_rel2)) {
                if ($wa1_rel2 && $wa1_rel1) {
                    return [
                        'code'=>1,
                        'msg' =>'发放成功'
                    ];
                }else{
                    return [
                        'code'=>0,
                        'msg' =>'报单奖励发放失败'
                    ];
                }
            }else{
                if ($wa1_rel1) {
                    return [
                        'code'=>1,
                        'msg' =>'发放成功'
                    ];
                }else{
                    return [
                        'code'=>0,
                        'msg' =>'报单奖励发放失败'
                    ];
                }
            }
            //return $first_award;
        }elseif ($ca_id == 9) {                    //特惠专区奖励
            $award6 = cache('setting')['award6']/100;
            $award7 = cache('setting')['award7']/100;
            $rel = model('user')->where('id',$user_first_id)->setInc('us_share_get',($total * $award6));
            $relall = model('user')->where('id',$user_first_id)->setInc('us_all_get',($total * $award6));
            if ($rel) {
                $data_w['or_id'] = $or_id;
                $data_w['us_id'] = $user_first_id;
                $data_w['wa_num'] = $total * $award6;
                $data_w['wa_type'] = 3;
                $data_w['wa_note'] = "特惠专区奖励发放";
                $data_w['level'] = "一级";
                $data_w['add_time'] = date('Y-m-d H:i:s');
                $wa1_rel1 = model('Wallet')->addInfo($data_w);
            }else{
                return [
                    'code'=>0,
                    'msg' =>'发送直推特惠专区奖励失败1'
                ];
            }
            if ($user_secend_id != 0) {
                $rel = model('user')->where('id',$user_secend_id)->setInc('us_share_get',($total * $award7));
                $relall = model('user')->where('id',$user_secend_id)->setInc('us_all_get',($total * $award7));
                if ($rel) {
                    $data_w['or_id'] = $or_id;
                    $data_w['us_id'] = $user_secend_id;
                    $data_w['wa_num'] = $total * $award7;
                    $data_w['wa_type'] = 3;
                    $data_w['wa_note'] = "特惠专区奖励发放";
                    $data_w['level'] = "二级";
                    $data_w['add_time'] = date('Y-m-d H:i:s');
                    $wa1_rel2 = model('Wallet')->addInfo($data_w);
                }else{
                    return [
                        'code'=>0,
                        'msg' =>'发送间接特惠专区奖励失败2'
                    ];
                }
            }
            if (isset($wa1_rel2)) {
                if ($wa1_rel2 && $wa1_rel1) {
                    return [
                        'code'=>1,
                        'msg' =>'发放成功'
                    ];
                }else{
                    return [
                        'code'=>0,
                        'msg' =>'特惠专区奖励发放失败'
                    ];
                }
            }else{
                if ($wa1_rel1) {
                    return [
                        'code'=>1,
                        'msg' =>'发放成功'
                    ];
                }else{
                    return [
                        'code'=>0,
                        'msg' =>'特惠专区奖励发放失败'
                    ];
                }
            }
        }
    }
    /**
     * 二级分销分红奖励发放  按月发放
     * @Author   sunpf
     * @DateTime 2019-4-18 11:29:45
     */
    public function share(){
        $us_array = model('user')->field('id')->select();
        //halt($us_array);
        
        foreach ($us_array as $key => $value) {
            $id_array[$value['id']] = $value['id']; 
        }
        //$arr = [][][];
        foreach ($id_array as $key => $value) {
            $a = model('user')->field('id')->where('us_pid',$value)->select();
            if (count($a) == 0) {
                $arr[$key] = $value;
            }else{
                 foreach ($a as $k => $v) {
                    $arr[$key][$v['id']] = $v['id'];
                 }
            }           
        }
        //halt($arr);
        foreach ($arr as $key => $value) {
            if (is_array($value)) {
                foreach ($value as $k => $v) {
                        $b = model('user')->field('id')->where('us_pid',$v)->select();
                        if (count($b) == 0) {
                            $ar[$key][$k] = $v; 
                        }else{
                            foreach ($b as $h => $r) {
                                $ar[$key][$r['id']] = $r['id'];
                                $ar[$key][$k] = $k;
                            }
                        }    
                }
                
            }else{
                $ar[$key] = $arr[$key];
            }
            
        }
        //halt($ar);
        foreach ($ar as $key => $value) {
            if (is_array($value)) {
                $month_award=[];
                foreach ($value as $k => $v) {
                    $total = 0;
                    
                    $first_person_nums = model('user')->where('us_pid','=',$key)->count();
                    if ($first_person_nums>=3 && $first_person_nums <= 17) {
                        $percent = 0.005;
                    }elseif($first_person_nums>=18 && $first_person_nums <= 59){
                        $percent = 0.01;
                    }elseif($first_person_nums>=60){
                        $percent = 0.025;
                    }else{
                        $percent = 0;
                    }
                    $map['us_id'] = $v;
                    $orders = model('order_detail')
                            ->alias('a')
                            ->join('order b','a.or_id = b.id')
                            ->where($map)
                            ->where('a.ca_id','<>',21)
                            ->whereIn('or_status','1,2,3')
                            ->where('year(or_add_time) = year(now()) and month(or_add_time) = month(now())')
                            ->field('or_pd_price,or_pd_num')
                            ->select();
                    if (count($orders) != 0) {
                        foreach ($orders as $h => $r) {
                            
                            $total += $r['or_pd_price'] * $r['or_pd_num'];
                        }
                        $month_award[] = $total;
                        //echo $key.'本月奖励为：'.$month_award;
                       
                    }
                    
                }
                //print_r($month_award);
                if (array_sum($month_award) != 0) {
                    echo $key.'本月奖励为:'.array_sum($month_award);
                    echo '<br>';
                }else{
                    echo $key.'本月无业绩2';
                    echo '<br>';
                }
                
            }else{
                echo $key.'本月无业绩1';
                echo '<br>';
            }
            
        }
    }
}